/**
 * 
 */
package com.nari.interfaces.service;

import java.util.Date;
import java.util.List;

import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.heatshare.dao.IAssignMeterDataStatisticsDao;
import com.nari.interfaces.dao.IAssignMeterDataBufferDao;
import com.nari.interfaces.dao.IHeatMeterDataBufferDao;
import com.nari.interfaces.dao.IMissionFailureLogDao;
import com.nari.interfaces.dao.IUploadMissionStateDao;
import com.nari.interfaces.decipher.common.warn.DecipherWarnConstants;
import com.nari.interfaces.decipher.common.warn.DecipherWarnLogger;
import com.nari.interfaces.model.IMissionFailureLog;
import com.nari.interfaces.model.IUploadMissionState;
import com.nari.platform.date.CalendarConstants;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2013-12-8
 */
public class MissionFailureLogServiceImpl extends GenericServiceImpl<IMissionFailureLog, String>
		implements IMissionFailureLogService {
	private IAssignMeterDataBufferDao iAssignMeterDataBufferDao;
	private IHeatMeterDataBufferDao iHeatMeterDataBufferDao;
	private IUploadMissionStateDao iUploadMissionStateDao;
	private IAssignMeterDataStatisticsDao iAssignMeterDataStatisticsDao;
	
	/**
	 * 审计缓存时间超过3小时的任务数据
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-8
	 */
	public void saveTimeoutBufferedDatasLog() throws Exception {
		System.out.println("--------------GPRS上传缓存库定时扫描|"+ new Date() +"---------------");
		//查询缓存时间超过3h的任务数据
		List<IUploadMissionState> missionList = iUploadMissionStateDao.queryExceedHoursMission(-3);
		if(missionList!=null && missionList.size()>0) {
			for(int index=0; index<missionList.size(); index++) {
				IUploadMissionState missionState = missionList.get(index);
				//抄表日期
				Date recordDate = CalendarUtils.parseStringToDate(missionState.getRecordTime(), CalendarConstants.dateFormat);
				int uploadedMissionCounter = iAssignMeterDataStatisticsDao.countStatisNumByUploadMission(missionState.getConcentratorNo(), recordDate);
				if(uploadedMissionCounter <= 0) {
					System.out.println(">>[断点续传超时："+missionState.getCreateTime()
							+"]任务信息："+missionState.getConcentratorNo()+"/"+missionState.getRecordTime());
					//任务上传失败审计日志
					StringBuffer auditLog = new StringBuffer();
					auditLog.append("[断点续传超时：");
					auditLog.append(CalendarUtils.parseDateTimeToString(missionState.getCreateTime()));
					auditLog.append("]");
					auditLog.append("任务信息：");
					auditLog.append(missionState.getConcentratorNo());
					auditLog.append("/");
					auditLog.append(missionState.getRecordTime());
					auditLog.append("超过3小时未续传数据！");
					IMissionFailureLog missionLog = DecipherWarnLogger.getMissionLogInstance(DecipherWarnConstants.logger_meterType_con, 
							missionState.getConcentratorNo(), missionState.getRecordTime(), 
							auditLog.toString(), "断点续传超时");
					this.getiGenericDao().saveEntity(missionLog);
				}
				//删除缓存的分配表数据
				this.iAssignMeterDataBufferDao.deleteMeterDataByMissionUID(missionState.getId());
				//删除缓存的热量表数据
				this.iHeatMeterDataBufferDao.deleteMeterDataByMissionUID(missionState.getId());
				//删除任务批次状态数据
				this.iUploadMissionStateDao.deleteEntityById(missionState.getId());
			}
		}
	}
	
	/**
	 * 分页查询上传失败的任务数据
	 * @param measureMeterNo 上传任务对应的集中器号/热表表号(可选)
	 * @param recordTimeStart 上传任务对应的抄表日期起始日期(可选)
	 * @param recordTimeEnd 上传任务对应的抄表日期结束日期(可选)
	 * @param pageCond 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-9
	 */
	public ResultWithPageCond<IMissionFailureLog> queryAuditLogPage(String measureMeterNo, 
			String recordTimeStart, String recordTimeEnd, PageCond pageCond) throws Exception {
		IMissionFailureLogDao iMissionFailureLogDao = (IMissionFailureLogDao) this.getiGenericDao();
		return iMissionFailureLogDao.queryAuditLogPage(measureMeterNo, recordTimeStart, recordTimeEnd, pageCond);
	}

	/**
	 * @return the iAssignMeterDataBufferDao
	 */
	public IAssignMeterDataBufferDao getiAssignMeterDataBufferDao() {
		return iAssignMeterDataBufferDao;
	}

	/**
	 * @param iAssignMeterDataBufferDao the iAssignMeterDataBufferDao to set
	 */
	public void setiAssignMeterDataBufferDao(
			IAssignMeterDataBufferDao iAssignMeterDataBufferDao) {
		this.iAssignMeterDataBufferDao = iAssignMeterDataBufferDao;
	}

	/**
	 * @return the iHeatMeterDataBufferDao
	 */
	public IHeatMeterDataBufferDao getiHeatMeterDataBufferDao() {
		return iHeatMeterDataBufferDao;
	}

	/**
	 * @param iHeatMeterDataBufferDao the iHeatMeterDataBufferDao to set
	 */
	public void setiHeatMeterDataBufferDao(
			IHeatMeterDataBufferDao iHeatMeterDataBufferDao) {
		this.iHeatMeterDataBufferDao = iHeatMeterDataBufferDao;
	}

	/**
	 * @return the iUploadMissionStateDao
	 */
	public IUploadMissionStateDao getiUploadMissionStateDao() {
		return iUploadMissionStateDao;
	}

	/**
	 * @param iUploadMissionStateDao the iUploadMissionStateDao to set
	 */
	public void setiUploadMissionStateDao(
			IUploadMissionStateDao iUploadMissionStateDao) {
		this.iUploadMissionStateDao = iUploadMissionStateDao;
	}

	/**
	 * @return the iAssignMeterDataStatisticsDao
	 */
	public IAssignMeterDataStatisticsDao getiAssignMeterDataStatisticsDao() {
		return iAssignMeterDataStatisticsDao;
	}

	/**
	 * @param iAssignMeterDataStatisticsDao the iAssignMeterDataStatisticsDao to set
	 */
	public void setiAssignMeterDataStatisticsDao(
			IAssignMeterDataStatisticsDao iAssignMeterDataStatisticsDao) {
		this.iAssignMeterDataStatisticsDao = iAssignMeterDataStatisticsDao;
	}
	
}
